package weaver.interfaces.unine;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FixedAssetsAction
 * @Description TODO
 * @Author ocean
 * @Date 2022/8/29 16:47
 * @Version 1.0
 */
public class SupplierStopAction extends BaseBean implements Action {

    private Logger log = LoggerFactory.getLogger( SupplierStopAction.class );

    @Override
    public String execute(RequestInfo requestInfo) {
        SupplierUtil supplierUtil = new SupplierUtil();
        String requestid = requestInfo.getRequestid();
        int formId = requestInfo.getRequestManager().getFormid();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>SupplierStopAction>>>>>" + requestid );
        try {
            Map mainTableDataMap = new HashMap();
            //取主表数据
            // 获取表单主字段信息
            Property[] properties = requestInfo.getMainTableInfo().getProperty();
            for (Property property : properties) {
                // 主字段名称
                String name = property.getName().toLowerCase();
                // 主字段对应的值
                String value = Util.null2String( property.getValue() );
                mainTableDataMap.put( name, value );
            }

            String action = "IStopSupplySV.svc/do";
            String url = getPropValue( "u9yk", "url" ) + action;


            String gfszzgs = Util.null2String( (String) mainTableDataMap.get( "gfszzgs" ) );
            String orgCode = supplierUtil.getCompanyCode( gfszzgs );
            String gysmc = Util.null2String( (String) mainTableDataMap.get( "gysmc" ) );


            rs.execute( "select u9gysbm from  uf_gysxx where id=" + gysmc );
            rs.next();
            String u9gysbm = Util.null2String( rs.getString( "u9gysbm" ) );


            JSONObject contextMap = supplierUtil.getContextInfo();
            Map<String, Object> inputMap = new HashMap<String, Object>();
            inputMap.put( "context", contextMap );
            inputMap.put( "orgCode", orgCode );
            inputMap.put( "code", u9gysbm );

            String back = supplierUtil.doSupplier( url, new JSONObject( inputMap ).toJSONString() );
            Map<String, Object> jsonMap = JSON.parseObject( back, HashMap.class );
            Map<String, Object> backJson = JSON.parseObject( Util.null2String( jsonMap.get( "d" ) ), HashMap.class );
            writeLog( "backJson=" + backJson );
            String status = Util.null2String( backJson.get( "Status" ) );
            writeLog( "status=" + status );
            String msg = Util.null2String( backJson.get( "Msg" ) );
            writeLog( "msg=" + msg );
            if ("1".equals( status )) {
                String code = Util.null2String( backJson.get( "Code" ) );
                writeLog( "code=" + code );
                return SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid( "1111113" );
                requestInfo.getRequestManager().setMessagecontent( msg );
                return FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
